package Welcome;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import ReadXML.LoadImageFromServer;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class DownloadAlert2 extends Canvas {

    private Start midlet;
    private Image bufferImage;
    private int hfColor = 0x00EA9414;
    private int dem;
    private int[] memory;
    private int x = 0;
    protected int linePadding = 1;
    protected int margin = 2;
    protected int padding = 2;
    protected Font font;
    protected int bgColor = 0x00EA9414;
    protected int borderWidth = 0;
    // will contain item splitted lines
    String[][] itemLines = null;
    // will hold items image parts
    Image[] images = null;
    // will hold selected item index
    public int selectedItem = 0;
    // these will hold item graphical properties
    int[] itemsTop = null;
    int[] itemsHeight = null;
    // these will hold List vertical scrolling
    int scrollTop = 0;
    final int SCROLL_STEP = 40;
    private boolean tha;
    private boolean change = true, isDown;
    private int tungDoDau;
    private int TextHeight;
    private int i, instanceKeyCode;
    private LoadImageFromServer lifs;
    int levelWant2Active, picsWant2Down;

    public DownloadAlert2(Start midlet) {
        this.setFullScreenMode(true);
        this.midlet = midlet;

        String[] items = new String[1];
        items[0] = "Bạn đã kích hoạt Level thành công. Tuy nhiên để chơi được bạn cần phải tải ảnh về.Bạn có muốn tải về không ?";

        bufferImage = Image.createImage(getWidth(), getHeight());
        itemLines = new String[items.length][];

        itemsTop = new int[itemLines.length];
        itemsHeight = new int[itemLines.length];

        for (i = 0; i < itemLines.length; i++) {
            font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);

            int w = getItemWidth();
            itemLines[i] = getTextRows((String) items[i], font, w);
        }
    }

    public int getItemWidth() {
        return getWidth() - 2 * borderWidth - 2 * padding - 2 * margin;
    }

    public String[] getTextRows(String text, Font font, int width) {
        char SPACE_CHAR = ' ';
//
        String VOID_STRING = "";
        String newLine = "<br />";
//
        int prevIndex = 0;
        int currIndex = text.indexOf(SPACE_CHAR);

        Vector rowsVector = new Vector();

        StringBuffer stringBuffer = new StringBuffer();

        String currentToken;

        String currentRowText = VOID_STRING;

        while (prevIndex != -1) {

//
            int startCharIndex = prevIndex == 0 ? prevIndex : prevIndex + 1;

            if (currIndex != -1) {
                currentToken = text.substring(startCharIndex, currIndex);
            } else {
                currentToken = text.substring(startCharIndex);
            }

            prevIndex = currIndex;

            currIndex = text.indexOf(SPACE_CHAR, prevIndex + 1);

            if (currentToken.length() == 0) {
                continue;
            }

            if (stringBuffer.length() > 0) {
                stringBuffer.append(SPACE_CHAR);
            }
            stringBuffer.append(currentToken);
//
            if (stringBuffer.toString().indexOf(newLine) >= 0) {
                String temp = stringBuffer.toString();

                stringBuffer.setLength(0);
                currentRowText = temp.substring(0, temp.indexOf(newLine));
                stringBuffer.append(temp.substring(temp.indexOf(newLine) + 8));
                if (currentRowText.length() > 0) {

                    rowsVector.addElement(currentRowText);
                    rowsVector.addElement("");
                }
                currentRowText = VOID_STRING;
                currentRowText = stringBuffer.toString();
//
            }

            if (font.stringWidth(stringBuffer.toString()) > width) {
//
                if (currentRowText.length() > 0) {

                    rowsVector.addElement(currentRowText);
//
                }
//
                stringBuffer.setLength(0);
//
                currentRowText = VOID_STRING;

                stringBuffer.append(currentToken);

                currentRowText = stringBuffer.toString();
            } else {
                currentRowText = stringBuffer.toString();
            }
//
        }
        if (currentRowText.length() > 0) {
            rowsVector.addElement(currentRowText);
        }
        String[] rowsArray = new String[rowsVector.size()];

        rowsVector.copyInto(rowsArray);

//
        return rowsArray;
    }

    protected void paint(Graphics g) {
        renderTheWorld();
        g.drawImage(bufferImage, 0, 0, Graphics.LEFT | Graphics.TOP);
    }

    protected void renderTheWorld() {
        Graphics g = bufferImage.getGraphics();
        g.setColor(0x00f8dfbd);
        g.fillRect(0, 0, getWidth(), getHeight());

        int top = 0;
        Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(f);
        g.setColor(0x00b0711f);
        g.fillRect(0, 0, getWidth(), f.getHeight() + 4);
        g.fillRect(0, getHeight() - f.getHeight() - 4, getWidth(), f.getHeight() + 4);
        g.setColor(0x00ffffff);
        g.drawString("Hướng dẫn", 2, 2, Graphics.LEFT | Graphics.TOP);
        g.drawLine(0, getHeight() - f.getHeight() - 4, getWidth(), getHeight() - f.getHeight() - 4);
        if (isDown) {
            g.drawString("Tải về", getWidth() - 1, getHeight() - 2, Graphics.RIGHT | Graphics.BOTTOM);
            g.drawString("Thoát", 1, getHeight() - 2, Graphics.LEFT | Graphics.BOTTOM);
        } else {
            g.drawString("OK", getWidth() - 1, getHeight() - 2, Graphics.RIGHT | Graphics.BOTTOM);
            g.drawString("Thoát", 1, getHeight() - 2, Graphics.LEFT | Graphics.BOTTOM);
        }
        g.translate(0, scrollTop);
        g.translate(0, font.getHeight() + 4);

        // loop List items
        for (int i = 0; i < itemLines.length; i++) {
            g.translate(0, -scrollTop);

            int itemRows = itemLines[i].length;

            int itemHeight = itemRows * font.getHeight() + linePadding * (itemRows - 1);
            itemsTop[i] = top;
            itemsHeight[i] = itemHeight;

            itemHeight += 2 * padding + 2 * borderWidth;
            if (change) {
                TextHeight += itemHeight;
            }
            g.translate(0, top);

            int textLeft = 0;
            Font font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
            g.setFont(font);
            g.setColor(0x000000);
            for (int j = 0; j < itemRows; j++) {//
                if (j == 0) {
                    textLeft = 10;
                } else {
                    textLeft = 5;
                }
                g.drawString(itemLines[i][j], textLeft, margin + borderWidth + padding + j * (linePadding + font.getHeight()) + x, Graphics.TOP | Graphics.LEFT);
//
            }
            g.translate(0, -top);
            top += itemHeight + 2 * margin;
        }
    }

    protected void pointerPressed(int x, int y) {
        if (x <= getWidth() && x >= getWidth() - font.stringWidth("OK") - 10) {
            if (y <= getHeight() && y >= getHeight() - font.getHeight() - 10) {
                midlet.disManager.pushDisplayable(this);
                isDown = true;
                if (DrawChoice.tt) {
                    DrawChoice.tangToc = true;
                }
                if (DrawChoice.kd) {
                    DrawChoice.khoiDong = true;
                }

                if (DrawChoice.vd) {
                    DrawChoice.veDich = true;
                }

                lifs = new LoadImageFromServer(midlet);
                lifs.parse();
                midlet.disManager.pushDisplayable(lifs);
            }
        } else if (x >= 0 && x <= font.stringWidth("Thoát" + 10)) {
            itemLines = null;
            itemsHeight = null;
            itemsTop = null;
            bufferImage = null;
            midlet.disManager.Home();
        }
    }

    protected void keyPressed(int keyCode) {
        instanceKeyCode = KeyCodeAdapter.getInstance().adoptKeyCode(keyCode);

        if (instanceKeyCode == KeyCodeAdapter.SOFT_KEY_LEFT) {
            itemLines = null;
            itemsHeight = null;
            itemsTop = null;
            bufferImage = null;
            midlet.disManager.Home();
        } else if (instanceKeyCode == KeyCodeAdapter.SOFT_KEY_RIGHT) {
            midlet.disManager.pushDisplayable(this);
            isDown = true;

            if (DrawChoice.tt) {
                DrawChoice.tangToc = true;
                DrawChoice.demBai = 0;
                DrawChoice.khoiDong = DrawChoice.kd = false;
                DrawChoice.veDich = DrawChoice.vd = false;
                QuestionList.imgId = 1;
            }
            if (DrawChoice.kd) {
                DrawChoice.khoiDong = true;
                DrawChoice.tangToc = DrawChoice.tt = false;
                DrawChoice.veDich = DrawChoice.vd = false;
                QuestionList.imgId = 1;
            }

            if (DrawChoice.vd) {
                DrawChoice.veDich = true;
                DrawChoice.khoiDong = DrawChoice.kd = false;
                DrawChoice.tangToc = DrawChoice.tt = false;
                QuestionList.imgId = 1;
            }

            lifs = new LoadImageFromServer(midlet);
            lifs.parse();
            midlet.disManager.pushDisplayable(lifs);
        }
    }
}
